﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using University.Workshop.DataAccess;
using University.Workshop.Business;
using System.Threading;
using University.Workshop.Model;
using System.Globalization;
using University.Workshop.DataAccess.LanguagePack.ClientLanguagePack;

namespace University.Workshop.ClientApp
{
    /// <summary>
    /// Interaction logic for littleRace.xaml
    /// </summary>
    public partial class littleRace : Window
    {
        bool wasPlaying;
        int quantityCardsUsed;
        List<string> cards;
        Random random = new Random();
        int cardSelectedItem;
        string cardSelectedName;
        bool continuePlayingWith;
        string myCar;
        User user;
        long amount;
        Image myImageCar;
        University.Workshop.Business.BetProperties business;
        string myCard;
        ThreadStart threadPrincipal;
        Thread thread;
        Mutex mutex;
        Mutex mutexBack;
        Client client;
        public bool Banned = false;
        string language;
        public littleRace(User userReceived, string carReceived, long amountReceived,string languageReceived)
        {
            
            InitializeComponent();
            language = languageReceived;
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(language);
            ApplyLanguage();
            wasPlaying = false;
            client = new Client();
            labelChip.Content = amountReceived;
            business = new BetProperties();
            quantityCardsUsed = 30;
            continuePlayingWith = true;
            user = userReceived;
            myCar = carReceived;
            amount = amountReceived;
            labelTotal.Content = amount;
            textSet.Text = "0";
            labelSet.Content = "0";
            myImageCar = new Image();
            business.Total = amountReceived;
            mutex = new Mutex();
            mutexBack = new Mutex();
            if (myCar == "carBlue")
            {
                myCard = "cardBlue";
                myImageCar = carBlue;

            }
            if (myCar == "carGreen")
            {
                myCard = "cardGreen";
                myImageCar = carGreen;
            }
            if (myCar == "carYellow")
            {
                myCard = "cardYellow";
                myImageCar = carYellow;
            }

            if (myCar == "carPink")
            {
                myCard = "cardPink";
                myImageCar = carPink;
            }

            if (myCar == "carRed")
            {
                myCard = "cardRed";
                myImageCar = carRed;
            }
            imageThis.Margin = new Thickness(imageThis.Margin.Left, myImageCar.Margin.Top, 0, 0);
            if (amount == 0)
            {
                textSet.IsEnabled = false;
                buttonBet.IsEnabled = false;
                buttonCancel.IsEnabled = false;
                buttonDown.IsEnabled = false;
                buttonUp.IsEnabled = false;
            }
            cards = new List<string>();
            for (int i = 0; i < 5; i++)
                cards.Add("cardBlue");
            for (int i = 0; i < 5; i++)
                cards.Add("cardYellow");
            for (int i = 0; i < 5; i++)
                cards.Add("cardRed");
            for (int i = 0; i < 5; i++)
                cards.Add("cardGreen");
            for (int i = 0; i < 5; i++)
                cards.Add("cardPink");
            for (int i = 0; i < 2; i++)
                cards.Add("crane");
            for (int i = 0; i < 3; i++)
                cards.Add("arrow");
        }

        private void ApplyLanguage()
        {
            groupBox1.Header = ClientLanguagePack.LittleRaceGroupBoxCar;
            groupBox2.Header = ClientLanguagePack.LittleRaceGroupBoxChoose;
            label7.Content = ClientLanguagePack.LittleRaceLabelYouGetIn;
            label1.Content = ClientLanguagePack.LittleRaceLabelChipsBet;
            label2.Content = ClientLanguagePack.LittleRaceLabelOnTable;
            label3.Content = ClientLanguagePack.LittleRaceLabelCattle;
            label4.Content = ClientLanguagePack.LittleRaceLabelLost;
            label5.Content = ClientLanguagePack.LittleRaceLabelTotal;
            button1.Content = ClientLanguagePack.LittleRaceButtonClaim;
            buttonBet.Content = ClientLanguagePack.LittleRaceButtonBet;
            buttonCancel.Content = ClientLanguagePack.LittleRaceButtonCancel;
            label8.Content = ClientLanguagePack.LittleRaceLabelRule1;
            label9.Content = ClientLanguagePack.LittleRaceLabelRule2;
        }

        private void moveCar(Object ImageObject, Object xObject, Object yObject, int quantity)
        {
            mutex.WaitOne();
            Image imageReceived = (Image)ImageObject;
            int x = (int)xObject;
            int y = (int)yObject;
            for (int i = x; i < x + quantity; i++)
            {
                Thread.Sleep(20);
                imageReceived.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
               new Action(delegate() { imageReceived.Margin = new Thickness(i, y, 0, 0); }));
            }

            mutex.ReleaseMutex();
        }
        private void moveCarBack(Object ImageObject, Object xObject, Object yObject, int quantity)
        {
            //http://msdn.microsoft.com/en-us/library/system.threading.mutex.releasemutex.aspx

            mutexBack.WaitOne();
            Image imageReceived = (Image)ImageObject;
            int x = (int)xObject;
            int y = (int)yObject;
            for (int i = x; i > x - quantity; i--)
            {
                Thread.Sleep(20);
                imageReceived.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
               new Action(delegate() { imageReceived.Margin = new Thickness(i, y, 0, 0); }));
            }
            mutexBack.ReleaseMutex();
        }
        private void changePicture(Image image, string path)
        {

            Uri uri = new Uri(path, UriKind.Relative);

            ImageSource imgSource = new BitmapImage(uri);

            image.Source = imgSource;
            image.IsEnabled = false;
        }
        private bool continuePlaying()
        {
            mutex.WaitOne();
            if (carBlue.Margin.Left < 330 && carYellow.Margin.Left < 330 && carGreen.Margin.Left < 330 && carPink.Margin.Left < 330 && carRed.Margin.Left < 330)
            {
                mutex.ReleaseMutex();
                return true;
            }
            mutex.ReleaseMutex();
            return false;

        }

        private bool changeValuesOfBet(string cardWatched)
        {
            if (cardWatched == "arrow")
            {
                long value = business.Bet / 2 + 300;
                if (value > business.Total)
                {
                    business.Lost = business.Lost + business.Total;
                    business.Total = 0;
                }
                else
                {
                    business.Lost = business.Lost + value;
                    business.Total = business.Total - value;
                }

                return true;
            }
            if (cardWatched == "crane")
            {
                business.Cattle = business.Cattle + 200 + business.Bet * 2;
                business.Total = business.Total + 200 + business.Bet * 2;
                return true;
            }
            if (cardWatched == myCard)
            {
                business.Cattle = business.Cattle + 100 + (business.Bet * 20 / 100);
                business.Total = business.Total + 100 + (business.Bet * 20 / 100);
                return true;
            }
            else
            {
                long value = 100 + business.Bet * 30 / 100;
                if (value > business.Total)
                {
                    business.Lost = business.Lost + business.Total;
                    business.Total = 0;
                    return true;
                }
                else
                {
                    business.Lost = business.Lost + value;
                    business.Total = business.Total - value;
                    return true;
                }
            }

        }
        private void chooseARandomPicture(Image image)
        {
            wasPlaying = true;
            continuePlayingWith = continuePlaying();

            if (continuePlayingWith)
            {
                cardSelectedItem = random.Next(quantityCardsUsed);
                if (quantityCardsUsed > 0)
                    quantityCardsUsed--;

                cardSelectedName = cards[cardSelectedItem];
                cards.Remove(cardSelectedName);
                //TODO     change pTH       
                string _cardSelectedName = "Images/" + cardSelectedName + ".jpg";
                changePicture(image, _cardSelectedName);

                if (string.Equals(cardSelectedName, "cardYellow"))
                {
                    int x = (int)carYellow.Margin.Left;
                    int y = (int)carYellow.Margin.Top;
                    threadPrincipal = delegate { moveCar(carYellow, x, y, 50); };
                    thread = new Thread(threadPrincipal);
                    thread.Start();
                }
                if (string.Equals(cardSelectedName, "cardBlue"))
                {
                    int x = (int)carBlue.Margin.Left;
                    int y = (int)carBlue.Margin.Top;
                    threadPrincipal = delegate { moveCar(carBlue, x, y, 50); };
                    thread = new Thread(threadPrincipal);
                    thread.Start();
                }
                if (string.Equals(cardSelectedName, "cardPink"))
                {
                    int x = (int)carPink.Margin.Left;
                    int y = (int)carPink.Margin.Top;
                    threadPrincipal = delegate { moveCar(carPink, x, y, 50); };
                    thread = new Thread(threadPrincipal);
                    thread.Start();
                }
                if (string.Equals(cardSelectedName, "cardGreen"))
                {
                    int x = (int)carGreen.Margin.Left;
                    int y = (int)carGreen.Margin.Top;
                    threadPrincipal = delegate { moveCar(carGreen, x, y, 50); };
                    thread = new Thread(threadPrincipal);
                    thread.Start();
                }
                if (string.Equals(cardSelectedName, "cardRed"))
                {
                    int x = (int)carRed.Margin.Left;
                    int y = (int)carRed.Margin.Top;
                    threadPrincipal = delegate { moveCar(carRed, x, y, 50); };
                    thread = new Thread(threadPrincipal);
                    thread.Start();
                }
                if (string.Equals(cardSelectedName, "arrow"))
                {
                    int x = (int)myImageCar.Margin.Left;
                    int y = (int)myImageCar.Margin.Top;

                    if (x > 165)
                    {
                        if (x < 180)
                        {
                            threadPrincipal = delegate { moveCarBack(myImageCar, x, y, 50); };
                            thread = new Thread(threadPrincipal);
                            thread.Start();
                        }
                        else
                        {
                            threadPrincipal = delegate { moveCarBack(myImageCar, x, y, 100); };
                            thread = new Thread(threadPrincipal);
                            thread.Start();
                        }
                    }

                }
                if (string.Equals(cardSelectedName, "crane"))
                {
                    int x = (int)myImageCar.Margin.Left;
                    int y = (int)myImageCar.Margin.Top;
                    if (x < 340)
                    {
                        if (x > 290)
                        {
                            threadPrincipal = delegate { moveCar(myImageCar, x, y, 50); };
                            thread = new Thread(threadPrincipal);
                            thread.Start();
                        }
                        else
                        {
                            threadPrincipal = delegate { moveCar(myImageCar, x, y, 100); };
                            thread = new Thread(threadPrincipal);
                            thread.Start();
                        }
                    }

                }
                changeValuesOfBet(cardSelectedName);
                textSet.Text = "0";
                labelCattle.Content = business.Cattle;
                labelLost.Content = business.Lost;
                labelOnTable.Content = "0";
                labelSet.Content = "0";
                labelTotal.Content = business.Total;
                business.Bet = 0;
            }
            else
            {
                this.Close();
            }

        }
        public void verifyIfWon()
        {
            long value;
            if (myImageCar.Margin.Left > 330)
            {
                value = user.Chips + business.Total + 2000;
                MessageBox.Show("You won extra 2000 chips! now you have:" + Convert.ToString(value) + "chips");
            }
            else
            {
                value = business.Total+user.Chips;
                MessageBox.Show("You lost, now you have:" + Convert.ToString(value));
            }
            client.SetGameStatus(user,"Races");
            user.Chips = value;
            client.Send("14 " + user.Name + " " + value);
            client.Receive();

        }
        private void image37_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {

        }

        private void image9_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {

        }
        void enlarge(Image image)
        {
            image.Width = image.Width + 20;
            image.Height = image.Height + 20;
        }
        void dwarf(Image image)
        {
            image.Width = image.Width - 20;
            image.Height = image.Height - 20;
        }
        private void card1_MouseEnter(object sender, MouseEventArgs e)
        {
            enlarge(card1);
        }

        private void card1_MouseLeave(object sender, MouseEventArgs e)
        {
            dwarf(card1);
        }

        private void card2_MouseEnter(object sender, MouseEventArgs e)
        {
            enlarge(card2);
        }

        private void card2_MouseLeave(object sender, MouseEventArgs e)
        {
            dwarf(card2);
        }

        private void card3_MouseEnter(object sender, MouseEventArgs e)
        {
            enlarge(card3);
        }

        private void card3_MouseLeave(object sender, MouseEventArgs e)
        {
            dwarf(card3);
        }

        private void card4_MouseEnter(object sender, MouseEventArgs e)
        {
            enlarge(card4);
        }
        private void card5_MouseEnter(object sender, MouseEventArgs e)
        {
            enlarge(card5);
        }

        private void card6_MouseEnter(object sender, MouseEventArgs e)
        {
            enlarge(card6);
        }
        private void card7_MouseEnter(object sender, MouseEventArgs e)
        {
            enlarge(card7);
        }
        private void card8_MouseEnter(object sender, MouseEventArgs e)
        {
            enlarge(card8);
        }
        private void card9_MouseEnter(object sender, MouseEventArgs e)
        {
            enlarge(card9);
        }
        private void card10_MouseEnter(object sender, MouseEventArgs e)
        {
            enlarge(card10);
        }
        private void card11_MouseEnter(object sender, MouseEventArgs e)
        {
            enlarge(card11);
        }
        private void card12_MouseEnter(object sender, MouseEventArgs e)
        {
            enlarge(card12);
        }
        private void card13_MouseEnter(object sender, MouseEventArgs e)
        {
            enlarge(card13);
        }
        private void card14_MouseEnter(object sender, MouseEventArgs e)
        {
            enlarge(card14);
        }
        private void card15_MouseEnter(object sender, MouseEventArgs e)
        {
            enlarge(card15);
        }
        private void card16_MouseEnter(object sender, MouseEventArgs e)
        {
            enlarge(card16);
        }
        private void card17_MouseEnter(object sender, MouseEventArgs e)
        {
            enlarge(card17);
        }
        private void card18_MouseEnter(object sender, MouseEventArgs e)
        {
            enlarge(card18);
        }
        private void card19_MouseEnter(object sender, MouseEventArgs e)
        {
            enlarge(card19);
        }
        private void card20_MouseEnter(object sender, MouseEventArgs e)
        {
            enlarge(card20);
        }
        private void card21_MouseEnter(object sender, MouseEventArgs e)
        {
            enlarge(card21);
        }
        private void card22_MouseEnter(object sender, MouseEventArgs e)
        {
            enlarge(card22);
        }
        private void card23_MouseEnter(object sender, MouseEventArgs e)
        {
            enlarge(card23);
        }
        private void card24_MouseEnter(object sender, MouseEventArgs e)
        {
            enlarge(card24);
        }
        private void card25_MouseEnter(object sender, MouseEventArgs e)
        {
            enlarge(card25);
        }
        private void card26_MouseEnter(object sender, MouseEventArgs e)
        {
            enlarge(card26);
        }
        private void card27_MouseEnter(object sender, MouseEventArgs e)
        {
            enlarge(card27);
        }
        private void card28_MouseEnter(object sender, MouseEventArgs e)
        {
            enlarge(card28);
        }
        private void card29_MouseEnter(object sender, MouseEventArgs e)
        {
            enlarge(card29);
        }
        private void card30_MouseEnter(object sender, MouseEventArgs e)
        {
            enlarge(card30);
        }
        private void card4_MouseLeave(object sender, MouseEventArgs e)
        {
            dwarf(card4);
        }
        private void card5_MouseLeave(object sender, MouseEventArgs e)
        {
            dwarf(card5);
        }
        private void card6_MouseLeave(object sender, MouseEventArgs e)
        {
            dwarf(card6);
        }
        private void card7_MouseLeave(object sender, MouseEventArgs e)
        {
            dwarf(card7);
        }
        private void card8_MouseLeave(object sender, MouseEventArgs e)
        {
            dwarf(card8);
        }
        private void card9_MouseLeave(object sender, MouseEventArgs e)
        {
            dwarf(card9);
        }
        private void card10_MouseLeave(object sender, MouseEventArgs e)
        {
            dwarf(card10);
        }
        private void card11_MouseLeave(object sender, MouseEventArgs e)
        {
            dwarf(card11);
        }
        private void card12_MouseLeave(object sender, MouseEventArgs e)
        {
            dwarf(card12);
        }
        private void card13_MouseLeave(object sender, MouseEventArgs e)
        {
            dwarf(card13);
        }
        private void card14_MouseLeave(object sender, MouseEventArgs e)
        {
            dwarf(card14);
        }
        private void card15_MouseLeave(object sender, MouseEventArgs e)
        {
            dwarf(card15);
        }
        private void card16_MouseLeave(object sender, MouseEventArgs e)
        {
            dwarf(card16);
        }
        private void card17_MouseLeave(object sender, MouseEventArgs e)
        {
            dwarf(card17);
        }
        private void card18_MouseLeave(object sender, MouseEventArgs e)
        {
            dwarf(card18);
        }
        private void card19_MouseLeave(object sender, MouseEventArgs e)
        {
            dwarf(card19);
        }
        private void card20_MouseLeave(object sender, MouseEventArgs e)
        {
            dwarf(card20);
        }
        private void card21_MouseLeave(object sender, MouseEventArgs e)
        {
            dwarf(card21);
        }
        private void card22_MouseLeave(object sender, MouseEventArgs e)
        {
            dwarf(card22);
        }
        private void card23_MouseLeave(object sender, MouseEventArgs e)
        {
            dwarf(card23);
        }
        private void card24_MouseLeave(object sender, MouseEventArgs e)
        {
            dwarf(card24);
        }
        private void card25_MouseLeave(object sender, MouseEventArgs e)
        {
            dwarf(card25);
        }
        private void card26_MouseLeave(object sender, MouseEventArgs e)
        {
            dwarf(card26);
        }
        private void card27_MouseLeave(object sender, MouseEventArgs e)
        {
            dwarf(card27);
        }
        private void card28_MouseLeave(object sender, MouseEventArgs e)
        {
            dwarf(card28);
        }
        private void card29_MouseLeave(object sender, MouseEventArgs e)
        {
            dwarf(card29);
        }
        private void card30_MouseLeave(object sender, MouseEventArgs e)
        {
            dwarf(card30);
        }
        private void verify()
        {
            long number;
            if (Int64.TryParse(textSet.Text, out number))
            {
                business.Bet = Convert.ToInt64(textSet.Text);
                if (business.Bet > business.Total)
                    MessageBox.Show("Incorrect amount");
                else
                {
                    labelOnTable.Content = business.Bet;
                    textSet.Text = "0";
                }
            }
            else {
                MessageBox.Show("Invalid argument");
                labelOnTable.Content = business.Bet;
                textSet.Text = "0";
            }
        }
        private void card1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            chooseARandomPicture(card1);
        }
        private void card2_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            chooseARandomPicture(card2);
        }
        private void card3_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            chooseARandomPicture(card3);
        }
        private void card4_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            chooseARandomPicture(card4);
        }
        private void card5_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            chooseARandomPicture(card5);
        }
        private void card6_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            chooseARandomPicture(card6);
        }
        private void card7_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            chooseARandomPicture(card7);
        }
        private void card8_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            chooseARandomPicture(card8);
        }
        private void card9_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            chooseARandomPicture(card9);
        }
        private void card10_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            chooseARandomPicture(card10);
        }
        private void card11_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            chooseARandomPicture(card11);
        }
        private void card12_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            chooseARandomPicture(card12);
        }
        private void card13_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            chooseARandomPicture(card13);
        }
        private void card14_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            chooseARandomPicture(card14);
        }
        private void card15_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            chooseARandomPicture(card15);
        }
        private void card16_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            chooseARandomPicture(card16);
        }
        private void card17_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            chooseARandomPicture(card17);
        }
        private void card18_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            chooseARandomPicture(card18);
        }
        private void card19_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            chooseARandomPicture(card19);
        }
        private void card20_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            chooseARandomPicture(card20);
        }

        private void card21_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            chooseARandomPicture(card21);
        }
        private void card22_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            chooseARandomPicture(card22);
        }
        private void card23_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            chooseARandomPicture(card23);
        }
        private void card24_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            chooseARandomPicture(card24);
        }
        private void card25_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            chooseARandomPicture(card25);
        }
        private void card26_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            chooseARandomPicture(card26);
        }
        private void card27_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            chooseARandomPicture(card27);
        }
        private void card28_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            chooseARandomPicture(card28);
        }
        private void card29_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            chooseARandomPicture(card29);
        }
        private void card30_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            chooseARandomPicture(card30);
        }

        private void textSet_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (string.IsNullOrEmpty(textSet.Text))
                textSet.Text = "0";
            else
            {
                labelSet.Content = textSet.Text;
            }
        }

        private void buttonUp_Click(object sender, RoutedEventArgs e)
        {
            long number;
            if (Int64.TryParse(Convert.ToString(labelSet.Content), out number))
            {
                long quantity = Convert.ToInt64(labelSet.Content);
                if (quantity < amount)
                {
                    textSet.Text = Convert.ToString(quantity + 1);
                    labelSet.Content = Convert.ToString(quantity + 1);
                }
            }
        }

        private void buttonDown_Click(object sender, RoutedEventArgs e)
        {
            long number;
            if (Int64.TryParse(Convert.ToString(labelSet.Content), out number))
            {
                long quantity = Convert.ToInt64(labelSet.Content);
                if (quantity > 0)
                {
                    textSet.Text = Convert.ToString(quantity - 1);
                    labelSet.Content = Convert.ToString(quantity - 1);
                }
            }
        }

        private void textSet_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                verify();
            }
            if (e.Key >= Key.D0 && e.Key <= Key.D9 || e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9)
                e.Handled = false;
            else
                e.Handled = true;
        }

        private void buttonCancel_Click(object sender, RoutedEventArgs e)
        {
            textSet.Text = "0";
            labelSet.Content = "0";
            business.OnTable = 0;
            business.Bet = 0;
            labelOnTable.Content = 0;
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (!Banned && wasPlaying)
            {
                if (continuePlaying())
                {
                    MessageBoxResult answer = MessageBox.Show("Are you sure? now you have " + Convert.ToInt32(user.Chips + business.Total), "", MessageBoxButton.YesNo);
                    if (answer == MessageBoxResult.Yes)
                    {
                        //save just chips than he has
                        user.Chips = user.Chips + business.Total;
                        if (thread != null)
                        {
                            if (thread.IsAlive)
                                thread.Abort();
                        }
                        client.Send("14 " + user.Name + " " + user.Chips);
                        client.Receive();
                    }
                    if (answer == MessageBoxResult.No)
                    {
                        e.Cancel = true;
                    }
                    else
                    {
                        client.SetGameStatus(user, "Races");
                    }

                }
                else
                {
                    verifyIfWon();
                }
            }
        }

        private void buttonBet_Click(object sender, RoutedEventArgs e)
        {
            verify();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void card1_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {

        }

    }
}
